<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>用户管理</title>
	<link th:include="common/header::copy">
<script type="text/javascript">

	function formatEdit(val,row,index){
        var str = '';
        if ($("#userroleset").length > 0) {
            str += $.formatString('<a  href="javascript:#" class="roles-easyui-linkbutton-edit"  onclick="roleSetFun(\'{0}\',\'{1}\');" >角色设置</a>',row.roles, row.uid);
        }
        if ($("#userupdate").length > 0) {
            str += $.formatString('<a href="javascript:void(0)" class="user-easyui-linkbutton-edit"  onclick="editUserFun(\'{0}\');" >编辑</a>', index);
        }
        if ($("#userdelete").length > 0) {
            str += $.formatString('<a href="javascript:void(0)" class="user-easyui-linkbutton-del"   onclick="deleteUserFun(\'{0}\');" >删除</a>', row.uid);
        }
        return str;
	}
	
	function roleSetFun(roles,userId){
		var rolesArr=roles.split(",");
		$("#dlg2").dialog("open").dialog("setTitle","选择角色");
		$("#dg2").datagrid({
			url:'/sys/role/getall',
			onLoadSuccess:function(data){
				var allRows=$("#dg2").datagrid("getRows");
				for(var i=0;i<allRows.length;i++){
					var name=allRows[i].rname;
					if($.inArray(name,rolesArr)>=0){
						$("#dg2").datagrid("checkRow",i);
					}
				}
			}
		});
		$("#userid").val(userId);
	}
	
	function searchUser(){
		$("#dg").datagrid('load',{
			"username":$("#s_account").val(),
			"sex":$("#s_sex").val()
		});
	}
	
	function resetValue(){
	    $("#uid").val("");
		$("#account").val("");
		$("#password").val("");
		$("#uname").val("");
		$("#createtime").val("");
		$("#lasttime").val("");
		$("#sex").val("");
		$("#remark").val("");
	}
	
	function closeUserDialog(){
		$("#dlg").dialog("close");
		resetValue();
	}
	
	function saveUser(){
		$("#fm").form("submit",{
			url:"/sys/user/save",
            method:"post",
			onSubmit:function(){
				return $(this).form("validate");
			},
			success:function(result){
				var result=eval('('+result+')');
				// easyui写法把Json改成字符串
				if(result.success){
					$.messager.show({
						title:'系统提示',
						msg:"保存成功",
						timeout:1000,
						showType:'show'
					});
					// $.messager.alert("系统提示","保存成功！");
					resetValue();
					$("#dlg").dialog("close");
					$("#dg").datagrid("reload");
				}else{
					$.messager.show({
						title:'系统提示',
						msg:result.info,
						timeout:1000,
						showType:'show'
					});
				}
			}
		});
	}
	
	function openUserAddDialog(){
		$("#uname").removeAttr("readonly");
		$("#dlg").dialog("open").dialog("setTitle","添加用户信息");
		url="/sys/user/save";
	}
	
	function editUserFun(index){
		$('#dg').datagrid('selectRow',index);
		var row=$("#dg").datagrid("getSelected");
		$("#dlg").dialog("open").dialog("setTitle","修改用户信息");
		$("#fm").form("load",row);
		$("#uname").attr("readonly","readonly");
	}
	
	function deleteUserFun(uid){
		$.messager.confirm("系统提示","您确定要删除这条数据吗?",function(r){
			if(r){
				$.post("/sys/user/delete",{userid:uid},function(result){
					if(result.success){
						$.messager.show({
							title:'系统提示',
							msg:"删除成功",
							timeout:1000,
							showType:'show'
						});
						$("#dg").datagrid("reload");
					}else{
						$.messager.show({
							title:'系统提示',
							msg:result.info,
							timeout:1000,
							showType:'show'
						});
					}
				},"json");
			}
		});
	}

	function saveRoleSet(){
		var userid=$("#userid").val();
		var selectedRows=$("#dg2").datagrid("getSelections");
		var strRoleIds=[];
		var strRoleNames=[];
		for(var i=0;i<selectedRows.length;i++){
			strRoleIds.push(selectedRows[i].rid);
            strRoleNames.push(selectedRows[i].rname);
		}
		var roleids=strRoleIds.join(",");
		var roleNames=strRoleNames.join(",");
		$.post("/sys/user/setrole",{roleids:roleids,userid:userid,roleNames:roleNames},function(result){
			if(result.success){
				closeRoleSetDialog();
				$("#dg").datagrid("reload");
			}else{
				$.messager.alert("系统提示","提交失败，请联系管理员！");
			}
		},"json");
	}
	
	function closeRoleSetDialog(){
		$("#dlg2").dialog("close");
	}

	function sexFor(value)
	{
		if (value == 1) {
			return '<span style=color:red; >男</span>';
		} else if (value == 2) {
			return '<span style=color:green; >女</span>';
		}
	};

	
</script>
</head>
<body style="margin: 1px">
<shiro:hasPermission name="sys:user:roleset">
    <input type="hidden" id="userroleset"/>
</shiro:hasPermission>
<shiro:hasPermission name="sys:user:delete">
    <input type="hidden" id="userdelete"/>
</shiro:hasPermission>
<shiro:hasPermission name="sys:user:save">
    <input type="hidden" id="userupdate"/>
</shiro:hasPermission>
	<table id="dg" title="用户管理" class="easyui-datagrid"
		   data-options="fit:true,fitColumns:true,pagination:true,rownumbers:true,singleSelect:true,
		   url:'/sys/user/list',method:'post',toolbar:'#tb'">
		<thead>
		<tr>
			<th field="uid" width="20" align="center" hidden="hidden">编号</th>
			<th field="account" width="50" align="center">用户名</th>
			<th field="password" width="50" align="center" hidden="true">密码</th>
			<th field="uname" width="50" align="center">姓名</th>
			<th field="sex" width="50" align="center" formatter="sexFor">性别</th>
			<th field="remark" width="50" align="center" hidden="true">备注</th>
			<th field="roles" width="100" align="center">拥有角色</th>
			<th field="aa" width="50" align="center" formatter="formatEdit">角色设置</th>
		</tr>
		</thead>
	</table>
	<div id="tb">
		<div shiro:hasPermission="sys:user:add" style="display: inline-block;">
			<a href="javascript:openUserAddDialog()" class="easyui-linkbutton" iconCls="icon-add" plain="true">添加</a>
            <a href="javascript:" class="easyui-linkbutton" data-options="plain:true" iconCls="icon-down"
			   onclick="$('#dg').datagrid('toExcel','user.xls')">下载</a>
		</div>
		<div shiro:hasPermission="sys:user:list" style="display: inline-block;float:right">
			帐号：<input type="text" id="s_account" size="20"/>
			男<input id="s_sex" type="radio" name="sex"/>
			女<input id="female" type="radio" name="sex"/>
			<a href="javascript:searchUser()" class="easyui-linkbutton" iconCls="icon-search" plain="true">搜索</a>
		</div>
	</div>
	
	
	<div id="dlg" class="easyui-dialog" style="width: 450px;height: 300px;padding: 10px 20px"
	closed="true" buttons="#dlg-buttons" data-options="onClose:function(){resetValue()}">
		<form id="fm" method="post">
            <input type="hidden" id="uid" name="uid">
			<table cellspacing="8px">
				<tr>
					<td>帐号：</td>
					<td>
						<input type="text" id="account" name="account" class="easyui-validatebox" required="true"/>
					</td>
				</tr>
				<tr>
					<td>姓名：</td>
					<td>
						<input type="text" id="uname" name="uname" class="easyui-validatebox" required="true" style="width: 250px"/>
					</td>
				</tr>
				<tr>
					<td>别名：</td>
					<td>
						<input type="text" id="nickname" name="nickname" class="easyui-validatebox" required="true" style="width: 250px"/>
					</td>
				</tr>
				<tr>
					<td valign="top">备注：</td>
					<td>
						<textarea rows="5" cols="40" id="remark" name="remark"></textarea>
					</td>
				</tr>
			</table>
		</form>
	</div>
	
	<div id="dlg-buttons">
		<a href="javascript:saveUser()" class="easyui-linkbutton" iconCls="icon-ok" >保存</a>
		<a href="javascript:closeUserDialog()" class="easyui-linkbutton" iconCls="icon-cancel" >关闭</a>
	</div>
	
	
	<div id="dlg2" class="easyui-dialog" style="width: 500px;height: 350px;"
	closed="true" buttons="#dlg-buttons2">
        <input type="hidden" id="userid" name="uid"/>
        <table id="dg2"  class="easyui-datagrid" fitColumns="true" fit="true">
		<thead>
		    <th field="cb" checkbox="true" align="center"></th>
			<th field="rid" width="20" align="left" hidden="true">编号</th>
			<th field="rname" width="100" align="left">角色名称</th>
			<th field="remark" width="200" align="left">备注</th>
		</thead>
	</table>
    </div>
	
	<div id="dlg-buttons2">
		<a href="javascript:saveRoleSet()" class="easyui-linkbutton" iconCls="icon-ok" >保存</a>
		<a href="javascript:closeRoleSetDialog()" class="easyui-linkbutton" iconCls="icon-cancel" >关闭</a>
	</div>
<script type="text/javascript">
    $(function () {
        $("#dg").datagrid({
            onLoadSuccess: function (data) {
                $('.roles-easyui-linkbutton-edit').linkbutton({text: '角色设置', plain: true, iconCls: 'icon-tip'});
                $('.user-easyui-linkbutton-edit').linkbutton({text: '编辑', plain: true, iconCls: 'icon-edit'});
                $('.user-easyui-linkbutton-del').linkbutton({text: '删除', plain: true, iconCls: 'icon-remove'});
                $('#dg').datagrid('fixRowHeight');
                // 加图标的时候必须这样写，在grid里面加图标
            }
        });
    });

</script>
</body>
</html>